package b3.widget.task
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class TaskSet extends BasicWidget
	{
		//同时显示个数
		private const CONTEMPORARY_TASK_NUM:int = 3;
		//任务列表
		private var taskList:TaskList;
		//当前正在进行任务
		private var ongoingTaskIds:Array;
		//位移
		private var offsetIndex:int;
		//图标容器
		private var iconContainer:Sprite;
		
		public function TaskSet(taskList:TaskList)
		{
			this.taskList = taskList;
			taskList.addEventListener(ModelEvent.REFRESH, onRefresh);
			taskList.addEventListener(ModelEvent.TASK_FINISH, onTaskFinish);
			var TaskUpArrow:Class = ResourceHelper.getInstance().getClassByName("TaskUpArrow");
			var taskUpArrow:BasicIcon = new BasicIcon(TaskUpArrow);
			taskUpArrow.addEventListener(MouseEvent.CLICK, onTaskUpArrowClick);
			taskUpArrow.x = 8;
			addChild(taskUpArrow);
			var TaskDownArrow:Class = ResourceHelper.getInstance().getClassByName("TaskDownArrow");
			var taskDownArrow:BasicIcon = new BasicIcon(TaskDownArrow);
			taskDownArrow.x = 8;
			taskDownArrow.y = 230;
			taskDownArrow.addEventListener(MouseEvent.CLICK, onTaskDownArrowClick);
			addChild(taskDownArrow);
			iconContainer = new Sprite();
			iconContainer.y = 35;
			addChild(iconContainer);
		}
		
		private function onTaskUpArrowClick(event:MouseEvent):void
		{
			if (offsetIndex > 0)
				offsetIndex--;
			refreshOngoingTaskIcons();
		} 
		
		private function onTaskDownArrowClick(event:MouseEvent):void
		{
			if (offsetIndex + CONTEMPORARY_TASK_NUM < ongoingTaskIds.length)
				offsetIndex++;
			refreshOngoingTaskIcons();
		}
		
		private function onRefresh(event:ModelEvent = null):void
		{
			ongoingTaskIds = new Array();
			for (var taskIdStr:String in Config.taskConfigs)
			{
				if (taskList.finishTasks[taskIdStr] == null)
				{
					ongoingTaskIds.push(parseInt(taskIdStr));
				}
			}
			ongoingTaskIds.sort(sortTask);
			refreshOngoingTaskIcons();
		}
		
		private function sortTask(a:int, b:int):int
		{
			var taskA:TaskConfig = Config.taskConfigs[a];
			var taskB:TaskConfig = Config.taskConfigs[b];
			if (taskA.sortIndex < taskB.sortIndex)
				return -1;
			else if (taskA.sortIndex > taskB.sortIndex)
				return 1;
			else
				return 0;
		}
		
		private function refreshOngoingTaskIcons():void
		{
			while (iconContainer.numChildren > 0)
				iconContainer.removeChildAt(0);
			for (var i:int = offsetIndex; i < Math.min(ongoingTaskIds.length, offsetIndex + CONTEMPORARY_TASK_NUM); i++)
			{
				var taskIcon:TaskIcon = new TaskIcon(ongoingTaskIds[i]);
				taskIcon.y = (i - offsetIndex) * 65;
				iconContainer.addChild(taskIcon);
			}
		}
		
		private function onTaskFinish(event:ModelEvent):void
		{
			onRefresh(event);
			var data:Object = event.data;
			var taskAwards:Array = new Array();
			for (var str:String in data['awards'])
			{
				var obj:Obj = new Obj();
				obj.loadbyObject(data['awards'][str]);
				taskAwards.push(obj);
			}
			var taskAccomplishBox:TaskAccomplishBox = new TaskAccomplishBox(data['taskId'], taskAwards);
			taskAccomplishBox.x = -taskAccomplishBox.width / 2;
			taskAccomplishBox.y = -taskAccomplishBox.height / 2;
			WidgetManager.getInstance().popOutOneByOne(taskAccomplishBox);
		}
	}
}